package com.example.publish;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitPublishSubscribeConfig {
    /**
     * Queue 可以有4个参数
     *      1.name           队列名
     *      2.durable       持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
     *      3.auto-delete   表示消息队列没有在使用时将被自动删除 默认是false
     *      4.exclusive     表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean
    public Queue createPublishSubscribeQueueA() {
        return new Queue("PublishSubscribeQueueA",true);
    }
    @Bean
    public Queue createPublishSubscribeQueueB() {
        return new Queue("PublishSubscribeQueueB",true);
    }
    @Bean
    public Queue createPublishSubscribeQueueC() {
        return new Queue("PublishSubscribeQueueC",true);
    }

    @Bean
    public FanoutExchange  publishSubscribeExchange() {
        //配置广播路由器
        return new FanoutExchange("PublishSubscribeExchange");
    }

    @Bean
    public Binding bingQueueAToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueA()).to(publishSubscribeExchange());
    }

    @Bean
    public Binding bingQueueBToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueB()).to(publishSubscribeExchange());
    }
    @Bean
    public Binding bingQueueCToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueC()).to(publishSubscribeExchange());
    }
}
